
<body>

    <div class="warp h">
        <!--header-->
        <header class="hd">

        <?php if(! isset($goods_id)): ?>
            <a href="<?php echo $base_url; ?>home/order/shoppingcar" class="l-icon only-icon">
        <?php else: ?>
            <a href="<?php echo $base_url; ?>home/goods/goodsDetail?goods_id=<?php echo $goods_id; ?>" class="l-icon only-icon">
        <?php endif;?>
            <i class="iconfont icon-left1"></i>            
            
        </a>

            <p class="tit2">
                订单填写
            </p>

        </header>
        <div class="order-address sel">
            <a href="<?php echo $base_url; ?>home/<?php if(empty($defaultAddressInfo)){echo 'address/add';}else{echo 'address';} ?>?data=<?php echo urlencode($dataFromGet); ?>">
                <?php if( ! empty($defaultAddressInfo)):?>
                <div class="l">
                    <i class="iconfont icon-shouhuodizhi"></i>
                </div>
                <div class="r">
                    <p>收货人：<?php echo $defaultAddressInfo['consignee'];?> <?php echo $defaultAddressInfo['mobile'];?></p>
                    <p>收货地址：<?php echo $defaultAddressInfo['full_address'];?></p>
                </div>
                <?php else:?>
                    <div class="l">
                    <!--<i class="iconfont icon-shouhuodizhi"></i>-->
                    </div>
                    <div class="r">                    
                    <p class="null_txt">您的收货地址为空，点此添加收货地址</p>
                    </div>
                <?php endif;?>
                <i class="icon iconfont icon-left"></i>

            </a>
        </div>

        <div class="pay-bar border-none">
            <ul>
                <li class="tit">支付方式</li>
                <?php if(empty($openid)): ?>
                <li>
                    <a href="#11" class="sel-pay">
                        <span class="l"><i class="iconfont icon-zhifu"></i>支付宝</span>
                        <span class="radio">
                            <i class="iconfont myicon icon-gou" data-paytype="3"></i>
                        </span>
                    </a>
                </li>
                <?php else: ?>
                  <li>
                    <a href="#11"  class="sel-pay">
                        <span class="l"><i class="iconfont icon-ananzuiconv267"></i>微信</span>
                        <span class="radio">
                            <i class="iconfont myicon icon-gou" data-paytype="2"></i>
                        </span>
                    </a>
                </li>
                <?php endif; ?>
            </ul>
        </div>
		<div class="line-box"></div>

        <ul class="order-list type03">
          
            <?php foreach ($goodsInfo as $k => $v) :?>
            <li>
                <!-- <div class="line-box"></div> -->
                <div class="order-itme">
                    <div class="order-box-1" data-goods_id="<?php echo $v['goods_id']; ?>">
                        <div class="l">
                            <img src="<?php echo $base_url.$v['img_path'];?>" alt="<?php echo $v['img_name'];?>">
                        </div>
                        <div class="r">
                            <p class="tit">
                                <?php echo $v['name']?>
                            </p>
                            <p class="ms-tit" style="display: none;">
                                颜色分类：黄色；
                            </p>
                            <p class="price-num">
                                <span>¥</span><?php echo $v['price'];?>
                                <span class="num" data-amount="<?php echo $v['amount'];?>">x<?php echo $v['amount'];?></span>
                            </p>
                        </div>
                    </div>
                </div>
            </li>
            <?php endforeach; ?>
            <li>
                <div class="order-itme">               
                    <div class="order-box-2">
                         <span class="r">
                            快递&nbsp;&nbsp;&nbsp;<span>免邮</span>
                        </span>
                        <span class="l">
                            配送方式
                        </span>
                    </div>           
                    <div class="order-box-2">
                        <span class="tit" >买家备注：</span>
                        <input class="remarks" type="text" placeholder="选填，对本次交易订单的说明" id="leave">

                    </div>
                </div>         
            </li>
        </ul>
        <div class="line-box"></div>

        <div class="pd-box"></div>
        <div class="sub-order-ft">
            <div class="pricesum">
                合计: <span>¥<?php echo $price; ?></span>
            </div>
            <a class="sub-btn <?php  if(empty($defaultAddressInfo)){echo 'err';}?>" id="submit" data-enable="1">
                提交订单
            </a>
        </div>

    </div>

    <script src="/static/dist/js/base.js"></script>

    <script>
        var payType = 3;
        // 矫正rem
        function correctrem() {
            var rem_full_width = $("<div id='rem-test' style='width:7.5rem'></div>").appendTo($('body')).width();
            var device_width = Math.min(document.documentElement.clientWidth, 640);
            if (rem_full_width !== device_width) {
                document.documentElement.style.fontSize = (device_width / 7.5) * (device_width / rem_full_width) + 'px';
            }
            $('#rem-test').remove();
        }

        function getQueryString(name) { 
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
            var r = window.location.search.substr(1).match(reg); 
            if (r != null) return unescape(r[2]); return null; 
        } 


		$('#leave').on('input',function(){
			if($(this).val().length>30){
				$(this).val($(this).val().slice(0,30))
			}
		})
        //清理购物车缓存
        function clearCartStorage()
        {
            var goods = JSON.parse(getQueryString('data'));
            // var cart_goods = JSON.parse(localStorage.getItem('cartData'));
			var cart_goods = getCookie('cartData') || [];		

            if(cart_goods != null && goods != null)
            {
                for(var i = 0;i < goods.length;i++)
                {
                    var goods_id = goods[i].goods_id;
                    for(var j = 0;j < cart_goods.length;j++)
                    {
                        if(goods_id == cart_goods[j].id)
                        {
                            cart_goods.splice(j, 1);
                        }
                    }
                }
                // localStorage.setItem('cartData', JSON.stringify(cart_goods));
                // localStorage.setItem('cartData', JSON.stringify(cart_goods));
				setCookie('cartData',cart_goods,2)
            }
        }

		function setCookie(cname, cvalue, exdays) {
				cvalue = JSON.stringify(cvalue)

				var d = new Date();
				d.setTime(d.getTime() + (exdays*60*60*1000));
				var expires = "expires="+d.toUTCString();
				document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";  
			}
		function getCookie(cname) {
			var name = cname + "=";
			var ca = document.cookie.split(';');
			for(var i=0; i<ca.length; i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1);
				if (c.indexOf(name) != -1){
					return JSON.parse(c.substring(name.length, c.length));
				} 
			}
			return "";
		}
        correctrem();
        $(function () {
             $(".sel-pay").hammer().bind("tap", function (ev) {
                 var $this_i = $(this).find('.radio i');   
                 console.log($this_i.hasClass("icon-gou"))              
                 if(!$this_i.hasClass("icon-gou")){
                    $(".radio i").removeClass("icon-gou");
                    $this_i.addClass("icon-gou");
                 }
                  
             });

             $('#submit').on('click', function(){
                //data-enable 属性防止重复点击
                if($(this).data('enable') == 1)
                {
                    $(this).data('enable', 0);
                    payType = $('.icon-gou').attr('data-paytype');
                    var selArr=[];
                    $('.order-box-1').each(function(){
                        var selObj = {};
                        selObj.goods_id = $(this).attr('data-goods_id');
                        selObj.quantity = $(this).find('.num').attr('data-amount');
                        selArr.push(selObj);
                    });
                    
                    $.post("<?php echo $base_url; ?>home/order/confirm", 
                            { paytype:payType, goods:selArr, leave:$('#leave').val()},  
                           function(data){  
                             $('#submit').data('enable', 1);
                             if(data.errcode != 0)
                             {
                                alert(data.errmsg);
                             } 
                             else
                             {
                                //下单成功，清理购物车本地缓存
                                clearCartStorage();
                                //支付宝支付
                                if(payType == 3){
                                    $.post("<?php echo $base_url; ?>ajax/payment/zhifbscpay",{order_id:data.orderid},function(result){
                                        // console.log(data);
                                        window.location.href = result.code_url;
                                    },"json");
                                }
                                
                                //微信支付
                                if(payType == 2){
                                    $.post("<?php echo $base_url; ?>ajax/payment/wxinoppay",{order_id:data.orderid},function(result){
                                        window.location.href = 'https://pay.swiftpass.cn/pay/jspay?token_id='+result.token_id+'&showwxtitle=1';
                                    },"json");
                                }
                             }
                           }, 
                            "json");
                }
             });

			
        })
    </script>

</body>

</html>
